package com.kl.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kl.domain.LineLogisticsStoreDTO;
import com.kl.domain.LogisticsStoreDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Line;
import com.kl.entity.LineLogisticsStore;
import com.kl.feign.LineServcieFeign;
import com.kl.feign.LogisticsStoreControllerFeign;
import com.kl.service.ILineLogisticsStoreService;
import com.kl.service.ILineService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-30
 */
@RestController
@RequestMapping("lineLogisticsStoreController")
public class LineLogisticsStoreController {

    @Autowired
    private ILineService lineService;
    @Autowired
    private ILineLogisticsStoreService lineLogisticsStoreService;
    @Autowired
    private LogisticsStoreControllerFeign logisticsStoreControllerFeign;
    @Autowired
    private LineServcieFeign lineServcieFeign;


    //物流信息增加接口
    @ApiOperation(value = "物流信息增加接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "lineId",value = "路线Id",required = true,paramType = "query")
    })
    @PostMapping
    public Result insertByLLId(@RequestParam("lineId") Long lineId) {// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            //所有的站点
            Result<List<LogisticsStoreDTO>> listResult = logisticsStoreControllerFeign.selectLogisticsStoreAll();
            if (listResult.getCode() == 1007){
                return Result.build(ConstantEnum.FaillBack).setResult("log服务熔断了");
            }else if (listResult.getCode() == 1004){
                return Result.build(ConstantEnum.ERROR);
            }
            List<LogisticsStoreDTO> resultList = listResult.getResult();
            List<String> olist = new ArrayList<>();
            for (int i = 0; i < resultList.size(); i++) {
                //将所有站点放进 olist 集合中
                olist.add(resultList.get(i).getStoreAddr());
            }
            //获取所有的路线
            Line line = lineService.getById(lineId);
            //循环所有的路线
            LineLogisticsStoreDTO LLSDTO = new LineLogisticsStoreDTO();
            //路线ID
            LLSDTO.setLineId(lineId);
            //获取当前对象经过站点的城市
            String lineCity = line.getLineCity();
            //线路经过的城市
            LLSDTO.setExperienceCity(lineCity);
            //线路预计时间
            LLSDTO.setEstimatedTime(line.getLineTime());
            //线路长度
            LLSDTO.setLineLength(line.getLineLength());
            List<String> cityList = JSONObject.parseArray(lineCity, String.class);
            //循环所有的城市
            for (int p = 0; p < cityList.size(); p++) {
                //循环所有的站点
                for (int o = 0; o < olist.size(); o++) {
                    //判断站点是否包含当前城市
                    if (olist.get(o).contains(cityList.get(p))) {
                        //站点ID
                        LLSDTO.setLogisticsStoreId(Long.valueOf(o + 1));
                        //站点顺序排序
                        LLSDTO.setSort(p + 1);
                        //站点仓库
                        LLSDTO.setStaffWarehouseId(String.valueOf(o + 1));
                    }
                }
                LineLogisticsStore lineLogisticsStore = new LineLogisticsStore();
                BeanUtils.copyProperties(LLSDTO, lineLogisticsStore);
                System.out.println(lineLogisticsStore);
                lineLogisticsStoreService.save(lineLogisticsStore);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //物流信息查询接口
    @ApiOperation(value = "物流信息查询接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "lineId",value = "路线Id",required = true,paramType = "query")
    })
    @GetMapping
    public Result<List<LineLogisticsStoreDTO>> selectByLineId(@RequestParam("lineId") Long lineId) {
        List<LineLogisticsStoreDTO> lineLogisticsStoreDTOS = null;
        try {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("line_id",lineId);
            List<LineLogisticsStore> list = lineLogisticsStoreService.list(wrapper);
            String string = JSONObject.toJSONString(list);
            lineLogisticsStoreDTOS = JSONArray.parseArray(string, LineLogisticsStoreDTO.class);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS).setResult(lineLogisticsStoreDTOS);
    }


    //物流信息删除接口
    @ApiOperation(value = "物流信息删除接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "lineId",value = "路线Id",required = true,paramType = "query")
    })
    @DeleteMapping
    public Result deleteByLineId(@RequestParam("lineId") Long lineId) {
        try {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("line_id",lineId);
            lineLogisticsStoreService.remove(wrapper);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }


    //物流信息查询下一个站点接口
    @ApiOperation(value = "物流信息查询下一个站点接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "lineId",value = "路线Id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "logisticsStoreId",value = "当前站点Id",required = true,paramType = "query")

    })
    @GetMapping("selectNextByLineId")
    public Result<LineLogisticsStoreDTO> selectNextByLineId(@RequestParam("lineId") Long lineId,@RequestParam("logisticsStoreId")Long logisticsStoreId) {
        LineLogisticsStoreDTO lineLogisticsStoreDTO = new LineLogisticsStoreDTO();
        try {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("line_id",lineId);
            List<LineLogisticsStore> list = lineLogisticsStoreService.list(wrapper);
            LineLogisticsStore lineLogisticsStore = null;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getLogisticsStoreId() == logisticsStoreId){
                    lineLogisticsStore = list.get(i + 1);
                }
            }
            BeanUtils.copyProperties(lineLogisticsStore,lineLogisticsStoreDTO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS).setResult(lineLogisticsStoreDTO);
    }


    /*//物流信息循环增加接口
    @ApiOperation(value = "物流信息循环增加接口")
    @PostMapping
    public Result insertByLLId(){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            //所有的站点
            String str = "北京市市辖区东城区,天津市市辖区南开区,河北省石家庄市市辖区,山西省太原市市辖区,内蒙古自治区呼和浩特市市辖区,辽宁省沈阳市市辖区,吉林省长春市市辖区,黑龙江省哈尔滨市市辖区,上海市,江苏省南京市市辖区,浙江省杭州市市辖区,安徽省合肥市市辖区,福建省福州市市辖区,江西省南昌市市辖区,山东省济南市市辖区,河南省郑州市市辖区,湖北省武汉市仙桃市,湖南省长沙市市辖区,广东省广州市市辖区,广西壮族自治区南宁市市辖区,海南省海口市,重庆市市辖区万州区,四川省成都市西昌市,贵州省贵阳市市辖区,云南省昆明市楚雄市,西藏自治区拉萨市林芝县,陕西省西安市市辖区,甘肃省兰州市市辖区,青海省西宁市市辖区,宁夏回族自治区银川市市辖区,新疆维吾尔自治区乌鲁木齐市石河子市";
            String[] split = str.split(",");
            List<String> olist = new ArrayList<>();
            //将所有站点保存到list集合里面
            for (int i = 0; i < split.length; i++) {
                olist.add(split[i]);
            }
            //获取所有的路线
            List<Line> list = lineService.list();
            //循环所有的路线
            for (int i = 0; i < list.size(); i++) {
                LineLogisticsStoreDTO LLSDTO = new LineLogisticsStoreDTO();
                Line line = list.get(i);
                //路线ID
                LLSDTO.setLineId(line.getId());
                //获取当前对象经过站点的城市
                String lineCity = line.getLineCity();
                //线路经过的城市
                LLSDTO.setExperienceCity(lineCity);
                //线路预计时间
                LLSDTO.setEstimatedTime(line.getLineTime());
                //线路长度
                LLSDTO.setLineLength(line.getLineLength());
                List<String> cityList = JSONObject.parseArray(lineCity, String.class);
                //循环所有的城市
                for (int p = 0; p < cityList.size(); p++) {
                    //循环所有的站点
                    for (int o = 0; o < olist.size(); o++) {
                        //判断站点是否包含当前城市
                        if (olist.get(o).contains(cityList.get(p))){
                            //站点ID
                            LLSDTO.setLogisticsStoreId(Long.valueOf(o + 1));
                            //站点顺序排序
                            LLSDTO.setSort(p + 1);
                            //站点仓库
                            LLSDTO.setStaffWarehouseId(String.valueOf(o + 1));
                        }
                    }
                    LineLogisticsStore lineLogisticsStore = new LineLogisticsStore();
                    BeanUtils.copyProperties(LLSDTO,lineLogisticsStore);
                    lineLogisticsStoreService.save(lineLogisticsStore);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }*/
}
